function queryStringify(obj, prefix) {
    let pairs = []
    for (let key in obj) {
        if (!obj.hasOwnProperty(key)) {
            continue
        }
        const value = obj[key]
        const enkey = encodeURIComponent(key)
        let pair
        if (typeof value === 'object') {
            pair = queryStringify(value, prefix ? prefix + '[' + enkey + ']' : enkey)
        } else {
            pair = (prefix ? prefix + '[' + enkey + ']' : enkey) + '=' + encodeURIComponent(value)
        }
        pairs.push(pair)
    }
    return pairs.join('&')
}

export default {
    /**
     *对象转formData字符串
     */
    queryStringify,
    /**
     * 是否为ipx类型设备
     * */
    isIpx() {
        const sysInfo = wx.getSystemInfoSync()
        const {model} = sysInfo
        const arr = ['iPhone X', 'iPhone XS', 'iPhone XS Max', 'iPhone XR']
        let isIpx = false
        arr.forEach(item => {
            if (model.includes(item)) {
                isIpx = true
            }
        })
        return isIpx
    },
    /**
     * 字符串超出显示...
     * @param {String} text @default '' 目标字符串
     * @param {String} num @default 0 截取长度
     * */
    textSplice(text = '', num = 0) {
        if (text && num > 0) {
            if (text.length > num) {
                return '...'.padStart(num + 3, text)
            }
            return text
        } else {
            return text
        }
    },
    /**
     * 下载临时文件路径或永久文件路径，不支持网络图片路径
     * @param {String} path @default 文件路径
     * */
    downImg(path) {
        return new Promise((resolve, reject) => {
            wx.saveImageToPhotosAlbum({
                filePath: path,
                success: res => {
                    wx.showToast({
                        icon: 'success',
                        title: '保存成功'
                    })
                    resolve(res)
                },
                fail: e => {
                    console.log(e)
                    if (e.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || e.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
                        wx.showModal({
                            title: '下载失败',
                            content: '您未授予保存到相册的权限，请在接下来的授权选项中选择 相册 > 点击打开，返回继续点击下载图片即可保存',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    wx.openSetting({})
                                } else if (res.cancel) {
                                    console.log('用户点击取消')
                                }
                            }
                        })
                    } else {
                        wx.showToast({
                            icon: 'none',
                            title: '保存失败'
                        })
                    }
                    reject(e)
                }
            })
        })
    },
    /**
     * 下载网络图片
     * @param {String} path @default 网络图片路径
     * */
    downHttpImg(url) {
        return new Promise((resolve, reject) => {
            wx.downloadFile({
                url,
                success: res => {
                    if (res.statusCode === 200) {
                        this.downImg(res.tempFilePath).then(res => {
                            resolve(res)
                        }).catch(err => {
                            reject(err)
                        })
                    } else {
                        reject(res)
                        wx.showToast({
                            icon: 'none',
                            title: '图片请求失败'
                        })
                    }
                },
                fail: e => {
                    reject(e)
                    wx.showToast({
                        icon: 'none',
                        title: '图片请求失败'
                    })
                }
            })
        })

    },
}
